<?php
if($this->do=='category'){
	$this->check_access('content_category');
	$this->template->in('category_html',$content->category_html());
	$this->template->out('content.category.list.php');
}
if($this->do=='category_add'){
	$category=array();
	$category['id']=0;
	$category['name']='';
	$category['dir']='';
	$category['keywords']='';
	$category['description']='';
	$category['index_template']='';
	$category['list_template']='';
	$category['view_template']='';
	$category['list_limit']=9;
	$category['sort']=0;
	$category['is_display']=1;
	$this->template->in('category',$category);
	$this->template->in('category_option',$content->category_option());
	$this->template->in('mode','insert');
	$this->template->out('content.category.info.php');
}
if($this->do=='category_insert'){
	$this->check_access('content_category');
	$category_name=empty($_POST['category_name'])?'':addslashes(trim($_POST['category_name']));
	$category_dir=empty($_POST['category_dir'])?'':addslashes(trim($_POST['category_dir']));
	$category_keywords=empty($_POST['category_keywords'])?'':addslashes(trim($_POST['category_keywords']));
	$category_description=empty($_POST['category_description'])?'':addslashes(trim($_POST['category_description']));
	$category_index_template=empty($_POST['category_index_template'])?'':addslashes(trim($_POST['category_index_template']));
	$category_list_template=empty($_POST['category_list_template'])?'':addslashes(trim($_POST['category_list_template']));
	$category_view_template=empty($_POST['category_view_template'])?'':addslashes(trim($_POST['category_view_template']));
	$category_list_limit=empty($_POST['category_list_limit'])?10:intval($_POST['category_list_limit']);
	$category_sort=empty($_POST['category_sort'])?0:intval($_POST['category_sort']);
	$category_is_display=empty($_POST['category_is_display'])?0:1;
	$parent_id=empty($_POST['parent_id'])?0:intval($_POST['parent_id']);
	if($parent_id==0){
		$category_deep=0;
	}else{
		$row=$this->db->row("SELECT category_deep FROM ".DB_PREFIX."content_category WHERE category_id=$parent_id");
		$category_deep=$row['category_deep']+1;
	}
	if($category_name==''){
		alert('分类名称不能为空');
	}
	if(empty($category_dir)&&!empty($category_name)){
		$category_dir=pinyin($category_name);
	}
	$array=array();
	$array['category_name']=$category_name;
	$array['category_dir']=$category_dir;
	$array['category_keywords']=$category_keywords;
	$array['category_description']=$category_description;
	$array['category_index_template']=$category_index_template;
	$array['category_list_template']=$category_list_template;
	$array['category_view_template']=$category_view_template;
	$array['category_deep']=$category_deep;
	$array['category_list_limit']=$category_list_limit;
	$array['category_sort']=$category_sort;
	$array['category_is_display']=$category_is_display;
	$array['parent_id']=$parent_id;
	$this->db->insert(DB_PREFIX."content_category",$array);
	$category_id=$this->db->id();
	if(!empty($_POST['into_menu'])){
		$array=array();
		$array['menu_name']=$category_name;
		$array['menu_link']=PATH.'a/'.$content->category_parent_path($category_id).'/';
		$array['menu_description']=$category_description;
		$array['menu_target']=0;
		$array['menu_sort']=$this->db->count("SELECT COUNT(*) from ".DB_PREFIX."menu")+1;
		$array['menu_status']=$category_is_display;
		$array['parent_id']=0;
		$this->db->insert(DB_PREFIX."menu",$array);
	}
	$content->category_path();
	$content->html_index();
	$content->html_category_index($category_id);
	$content->html_category_list($category_id);
	redirect('?action=content&do=category');
}
if($this->do=='category_edit'){
	$category_id=empty($_GET['category_id'])?0:intval($_GET['category_id']);
	$row=$this->db->row("SELECT * FROM ".DB_PREFIX."content_category WHERE category_id='$category_id' LIMIT 0,1");
	$category=array();
	$category['id']=$row['category_id'];
	$category['name']=$row['category_name'];
	$category['dir']=$row['category_dir'];
	$category['keywords']=$row['category_keywords'];
	$category['description']=$row['category_description'];
	$category['index_template']=$row['category_index_template'];
	$category['list_template']=$row['category_list_template'];
	$category['view_template']=$row['category_view_template'];
	$category['list_limit']=$row['category_list_limit'];
	$category['sort']=$row['category_sort'];
	$category['is_display']=$row['category_is_display'];
	$this->template->in('category',$category);
	$this->template->in('category_option',$content->category_option(0,$row['parent_id']));
	$this->template->in('mode','update');
	$this->template->out('content.category.info.php');
}
if($this->do=='category_update'){
	$this->check_access('content_category');
	$category_id=empty($_POST['category_id'])?0:intval($_POST['category_id']);
	$category_name=empty($_POST['category_name'])?'':addslashes(trim($_POST['category_name']));
	$category_dir=empty($_POST['category_dir'])?'':addslashes(trim($_POST['category_dir']));
	$category_dir_old=empty($_POST['category_dir_old'])?'':trim($_POST['category_dir_old']);
	$category_keywords=empty($_POST['category_keywords'])?'':addslashes(trim($_POST['category_keywords']));
	$category_description=empty($_POST['category_description'])?'':addslashes(trim($_POST['category_description']));
	$category_index_template=empty($_POST['category_index_template'])?'':addslashes(trim($_POST['category_index_template']));
	$category_list_template=empty($_POST['category_list_template'])?'':addslashes(trim($_POST['category_list_template']));
	$category_view_template=empty($_POST['category_view_template'])?'':addslashes(trim($_POST['category_view_template']));
	$category_list_limit=empty($_POST['category_list_limit'])?10:intval($_POST['category_list_limit']);
	$category_sort=empty($_POST['category_sort'])?0:intval($_POST['category_sort']);
	$category_is_display=empty($_POST['category_is_display'])?0:1;
	$parent_id=empty($_POST['parent_id'])?0:intval($_POST['parent_id']);
	if($parent_id==0){
		$category_deep=0;
	}else{
		$row=$this->db->row("SELECT category_deep FROM ".DB_PREFIX."content_category WHERE category_id=$parent_id");
		$category_deep=$row['category_deep']+1;
	}
	if($category_name==''){
		alert('分类名称不能为空');
	}
	if(empty($category_dir)&&!empty($category_name)){
		$category_dir=pinyin($category_name);
	}
	if($category_dir_old!=$category_dir){
		$path='a/'.str_replace($category_dir_old,'',$content->category_parent_path($category_id));
		@rename($path.'/'.$category_dir_old,$path.'/'.$category_dir);
	}
	$array=array();
	$array['category_name']=$category_name;
	$array['category_dir']=$category_dir;
	$array['category_keywords']=$category_keywords;
	$array['category_description']=$category_description;
	$array['category_index_template']=$category_index_template;
	$array['category_list_template']=$category_list_template;
	$array['category_view_template']=$category_view_template;
	$array['category_deep']=$category_deep;
	$array['category_list_limit']=$category_list_limit;
	$array['category_sort']=$category_sort;
	$array['category_is_display']=$category_is_display;
	$array['parent_id']=$parent_id;
	$this->db->update(DB_PREFIX."content_category",$array,"category_id=$category_id");
	$content->category_path();
	$content->html_index();
	$content->html_category_index($category_id);
	$content->html_category_list($category_id);
	redirect('?action=content&do=category');
}
//分类删除
if($this->do=='category_delete'){
	$category_id=empty($_GET['category_id'])?0:intval($_GET['category_id']);
	#删除该目录
	rm_dir(ROOT.'a/'.$content->category_parent_path($category_id));
	#读取内容并删除相关联的杂项
	$result=$this->db->result("SELECT article_id,article_image,article_file FROM ".DB_PREFIX."content_article WHERE category_id=$category_id");
	foreach($result as $row){
		if(!empty($row['article_image'])){
			@unlink(ROOT.'/'.$row['article_image']);
		}
		//提取该内容附属的附件文件名并删除
		$file=explode(",",$row['article_file']);
		foreach($file as $v){
			@unlink(ROOT."/".$v);
		}
		$this->db->delete(DB_PREFIX."content_comment","article_id=".$row['article_id']);//删除内容评论
	}
	$this->db->delete(DB_PREFIX."content_article","category_id=$category_id");//删除内容
	$this->db->delete(DB_PREFIX."content_category","category_id=$category_id");//删除分类
	redirect('?action=content&do=category');
}
//分类清理
if($this->do=='category_clear'){
	$category_id=empty($_POST['category_id'])?array():$_POST['category_id'];
	foreach($category_id as $value){
		if(!empty($value)){
			$dir='a/'.$content->category_parent_path($value);
			$folder=@opendir($dir);
			if($folder){
				while ($file=readdir($folder)){
					if($file!="."&&$file!=".."&&$file!="index.htm"){
						@unlink($dir."/".$file);
					}
				}
				closedir($folder);
			}
		}
	}
	redirect('?action=content&do=category');
}
if($this->do=='category_html'){
	$content->category_path();
	$category_id=empty($_POST['category_id'])?array():$_POST['category_id'];
	foreach($category_id as $id){
		if(!empty($id)){
			$content->html_category_index($id);
			$content->html_category_list($id);
			$result=$this->db->result("SELECT * FROM ".DB_PREFIX."content_article WHERE category_id=$id");
			if($result){
				foreach($result as $row){
					$content->html_article($row['article_id']);
				}
			}
			unset($result);

		}
	}
	unset($category_id);
	$content->html_index();
	redirect('?action=content&do=category');
}